const fontUtils = {
    // 字体加载
    loadFont(_fontName, _fontUrl) {
        if (fontUtils.checkFont(_fontName)) {
            console.warn('已有字体：', _fontName)
            return Promise.resolve()
        }
        let prefont = new FontFace(
            _fontName,
            'url(' + _fontUrl + ')'
        );
        return prefont.load().then(function (loaded_face) {
            document.fonts.add(loaded_face);
        }).catch(function (error) {
            console.error('字体加载失败', error, _fontName, _fontUrl)
        })
    },
    // 检测字体文件是否已加载
    checkFont(name) {
        let values = document.fonts.values();
        let isHave = false;
        let item = values.next();
        while (!item.done && !isHave) {
            let fontFace = item.value;
            if (fontFace.family == name) {
                isHave = true;
            }
            item = values.next();
        }
        return isHave;
    }
}

export default fontUtils
